import Http from './http'; // 根据实际路径调整

// 假设我们的http封装在一个名为HttpClient的类中
let httpClient = new Http('https://erp.uyun-iot.cn');
let appToken =""

interface ApiResponse<T> {
    code: number;
    msg: string;
    data: T;
  }

interface LoginData {
  username: string;
  password: string;
}



export async function login(loginData: LoginData) {
  try {
	  let url = `/uyun/v1/customer/user/login?username=${loginData.username}&password=${loginData.password}`
    const response = await httpClient.request<ApiResponse<any>>('POST', url, loginData);
    if (response.code === 0) {
      // console.log('登录成功', response.data);
	  let  result = response.data
	  appToken = result.token
	  httpClient.setHeader({token:result.token});
      return response.data;
    } else {
      console.error('登录失败', response.msg);
      throw new Error(response.msg);
    }
  } catch (error) {
    console.error('请求出错', error);
	uni.showToast({title: error.message || '登录失败',icon: 'none'});
    throw error;
  }
}

// 获取产品列表
export async function getProductList(params: any) {
  try {
	// let headers:string = {token:uni.getStorageSync('token')}
	let headers = {token:appToken}
    const response = await httpClient.request<ApiResponse<any>>('POST', '/uyun/v1/customer/product/list', params,headers);
    if (response.code === 0) {
      console.log('请求成功', response.data);
      return response.data;
    } else {
      console.error('请求失败', response.msg);
      throw new Error(response.msg);
    }
  } catch (error) {
    console.error('请求出错', error);
    throw error;
  }
}

// 获取设备列表
export async function getDevicesList(params: any) {
  try {
	// let headers:string = {token:uni.getStorageSync('token')}
	let headers = {token:appToken}
    const response = await httpClient.request<ApiResponse<any>>('POST', '/uyun/v1/customer/device/page', params,headers);
    if (response.code === 0) {
      // console.log('请求成功', response.data);
      return response.data;
    } else {
      console.error('请求失败', response.msg);
      throw new Error(response.msg);
    }
  } catch (error) {
    console.error('请求出错', error);
    throw error;
  }
}

// 添加产品
export async function addNewProduct(params: any) {
  try {
	// let headers:string = {token:uni.getStorageSync('token')}
	let headers = {token:appToken}
    const response = await httpClient.request<ApiResponse<any>>('POST', '/uyun/v1/customer/product/save', params,headers);
    if (response.code === 0) {
      console.log('请求成功', response.data);
      return response.data;
    } else {
      console.error('请求失败', response.msg);
      throw new Error(response.msg);
    }
  } catch (error) {
    console.error('请求出错', error);
    throw error;}
  
}

// 添加设备
export async function addDevices(productId:string,params: any) 
{
  try {

	// let headers:string = {token:uni.getStorageSync('token')}
	let headers = {token:appToken}
	let url =`/uyun/v1/customer/device/importbysn?productId=${productId}`
    const response = await httpClient.request<ApiResponse<any>>('POST',url, params,headers);
    if (response.code === 0) {
      console.log('请求成功', response.data);
      return response.data;
    } else {
      console.error('请求失败', response.msg);
	  uni.showToast({
	    title: error.message??"添加设备失败",
	    icon: 'fail'
	  }); 
      throw new Error(response.msg);
    }
  } catch (error) {
    console.error('请求出错', error);
	uni.showToast({
	  title: error??"添加设备失败",
	  icon: 'fail'
	}); 
    throw error;
  }
}

  // 获取单个设备信息
export async function getOnceDeviceInfo(params: any) 
{
    try {
      // let headers:string = {token:uni.getStorageSync('token')}
	  let headers = {token:appToken}
      const response = await httpClient.request<ApiResponse<any>>('POST', '/uyun/v1/customer/device/one', params,headers);
      if (response) {
        // console.log('请求成功', response.data);
        return response.data;
      } else {
        console.error('请求失败', response);
        throw new Error("fail");
      }
    } catch (error) {
      console.error('请求出错', error);
      throw error;
    }
}
  // 获取设备状态的详细信息
export async function getDevicesDeltail(params: any) 
{
    try {
      // let headers:string = {token:uni.getStorageSync('token')}
	  let headers = {token:appToken}
	  let newHttpClient = new Http('https://rtkv.air32.cn');
      const response = await newHttpClient.request<ApiResponse<any>>('GET', '/api/rtkv/gets', params,null);
      if (response) {
        // console.log('请求成功', response.data);
        return response.data;
      } else {
        console.error('请求失败', response);
        throw new Error("fail");
      }
    } catch (error) {
      console.error('请求出错', error);
      throw error;
    }
}

  // 更新设备信息
export async function updateDevice(params: any) 
{
    try {
      // let headers:string = {token:uni.getStorageSync('token')}
	  let headers = {token:appToken}
      const response = await httpClient.request<ApiResponse<any>>('POST', '/uyun/v1/customer/device/update', params,headers);
      if (response) {
        // console.log('请求成功 updateDevice', response.data);
        return response.data;
      } else {
        console.error('请求失败', response);
        throw new Error("fail");
      }

}

  // 获取日志
export async function getLogs(params: any) 
{
    try {
	  // let headers:string = {token:uni.getStorageSync('token')||""}
	  let headers = {token:appToken}
      const response = await httpClient.request<ApiResponse<any>>('POST', '/uyun/v1/customer/device/event/log/page', params,headers);
      if (response.code === 0) {
        // console.log('请求成功', response.data);
        return response.data;
      } else {
        console.error('请求失败', response.msg);
        throw new Error(response.msg);
      }
    } catch (error) {
      console.error('请求出错', error);
      throw error;
    }
}

// 获取用户信息
export async function getUserInfo() 
{
    try {
	  // let headers:string = {token:uni.getStorageSync('token')}
	  let headers = {token:appToken}
	  let url = `/uyun/v1/customer/user/getinfo`
      const response = await httpClient.request<ApiResponse<any>>('POST', url, null,headers);
	  if (response.code === 0) {
        console.log('请求成功', response.data);
        return response.data;
      } else {
        console.error('请求失败', response.msg);
        throw new Error(response.msg);
      }
    } catch (error) {
	  if(!error){
		return	
	  }
	  uni.showToast({
	    title: error['message']||"请求出错",
	    icon: 'fail'
	  });  
      throw error;
    }
}
  // 修改密码
export async function changePwd(params: any) 
{
    try {
	  // let headers:string = {token:uni.getStorageSync('token')||""}
	  let headers = {token:appToken}
	  let url = `/uyun/v1/customer/user/updatepassword?oldpassword=${params['oldpassword']}&newpassword=${params['newpassword']}`
      const response = await httpClient.request<ApiResponse<any>>('POST', url, params,headers);
	  if (response.code === 0) {
        console.log('请求成功', response.data);
        return response.data;
      } else {
        console.error('请求失败', response.msg);
        throw new Error(response.msg);
      }
    } catch (error) {
		if(!error){
			return
		}
      console.error('请求出错', error);
	  uni.showToast({
	    title: error['message']||"请求出错",
	    icon: 'fail'
	  });  
      throw error;
    }
}
